package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发售交易-委托下单填充相关参数：
 * 1.初始化委托单参数
 *
 * @author admin
 */
public class SaleOrderPaddingParamPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        String orderFlag = String.valueOf(param.get("trsOrder.orderFlag"));
        String proId = String.valueOf(param.get("trsOrder.proId"));
        String proNo = String.valueOf(param.get("trsOrder.proNo"));
        String proName = String.valueOf(param.get("trsOrder.proName"));

        log.info("start->>初始化委托单[" + orderFlag + "]-[" + proNo + "-" + proName + "]参数");
        param.put("trsOrder.isDeal", "F");//挂单初始化未成交
        param.put("trsOrder.isKill", "F");//挂单初始化未撤单

        List<DataMap> rulesListMap = getTransRulesList(param, proId);
        if (rulesListMap == null || rulesListMap.size() < 1) {
            log.info("无规则参数可补充");
            return null;
        }
        for (DataMap buyMap : rulesListMap) {

            String paramName = String.valueOf(buyMap.get("paramName"));
            String paramVal = String.valueOf(buyMap.get("paramNo"));
            String rulesNo = String.valueOf(buyMap.get("rulesNo"));
            if ("buy".equals(orderFlag)) {
                if ("order08".equals(rulesNo)) {
                    //买方手续费比例
                    log.info("补充规则[" + paramName + "-" + rulesNo + "]设置的[" + paramVal + "]");
                    param.put("trsOrder.feeRatio", paramVal);
                } else if ("order16".equals(rulesNo)) {
                    //手续费留存比例（分佣）
                    log.info("补充规则[" + paramName + "-" + rulesNo + "]设置的[" + paramVal + "]");
                    param.put("trsOrder.feeRatioKeep", paramVal);
                }
            }
            if ("sell".equals(orderFlag)) {
                if ("order09".equals(rulesNo)) {
                    //买方手续费比例
                    log.info("补充规则[" + paramName + "-" + rulesNo + "]设置的[" + paramVal + "]");
                    param.put("trsOrder.feeRatio", paramVal);
                } else if ("order17".equals(rulesNo)) {
                    //手续费留存比例（分佣）
                    log.info("补充规则[" + paramName + "-" + rulesNo + "]设置的[" + paramVal + "]");
                    param.put("trsOrder.feeRatioKeep", paramVal);
                }
            }
        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 查询商品交易规则
     *
     * @param param
     * @param proId
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<DataMap> getTransRulesList(Map<String, Object> param, String proId) {
        // String searchObjName, Map<String, Object> whereMap
//        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
//                .getBean("netProxyManager");
//        ParamsVo params = new ParamsVo();
//        params.setParams("doCode", "dymicSql");
//        params.setParams("sqlKeyId", "ByDymicSql");
//        params.setParams("resultType", "objInfo");
//        params.setKey("publicForSql");

        Map<String, Object> whereMap = new HashMap<String,Object>();
        //封装参数
        whereMap.put("trsRulesPro.w_showFlag", "T");
        whereMap.put("trsRulesPro.w_rulesType", "guadan");
        whereMap.put("trsRulesPro.w_constantType", "order_status");
        //		params.setParams("trsRulesPro.w_constantStatus", "10");
        whereMap.put("trsRulesPro.w_prodId", proId);
        //		params.setParams("trsRulesPro.w_paramNo", " is not null ");
        whereMap.put("orderBy", "trsRulesPro.rules_no asc");

        ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("list","s_trsRulesPro",whereMap);
        if(resultEntity1==null || resultEntity1.getEntity()==null){
            throw new BusinessException("查询规则失败！");
        }
        List<DataMap> list = (List<DataMap>)resultEntity1.getEntity();
        if(list==null || list.size()==0){
            throw new BusinessException("查询规则失败！");
        }
        return list;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

}
